$(function(){
	
//判断ios和安卓
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

//点赞
$('.tuwen_pinglun_comment_center').on('touchstart', 'span', function(ev) {
	var
		ev = ev || window.event,
		oTarget = ev.srcElement || ev.target,
		commentid = $(this).attr("commentid");
	var phoneNumber = ((document.URL).split("&")[1].split("=")[1]);
	var contentID = ((document.URL).split("&")[2].split("=")[1]);	
	
	if($(this).hasClass("tuwen_pinglun_praiseImg")) {
		$(this).removeClass("tuwen_pinglun_praiseImg").addClass('tuwen_pinglun_praiseImg_s');
		$(this).next().html(parseInt($(this).next().html()) + 1);
	}
	
	
	
	$.ajax({
			type:'GET', 
	        url:'http://dev.onetuu.com:8080/python/api/IosCommentLike',       //跨域到https://www.wp.com，另，https://test.com也算跨域	                                       //jsonp 类型下只能使用GET,不能用POST,这里不写默认为GET
	       	async:false,	
	        dataType:'jsonp',			                //指定为jsonp类型
	        data:{									    //数据参数						
				"commentID":commentid,
				"contentID":contentID,
				"phoneNumber":phoneNumber
	        },
	        success:function fcv(data){
	        	
	        }			
	})					
})

	//查看全部评论
$('.tuwen_pinglun_allTxt').on("singleTap", function() {
	var phoneKey    = ((document.URL).split("&")[0].split("=")[1]);
	var phoneNumber = ((document.URL).split("&")[1].split("=")[1]);
	var cateID = ((document.URL).split("&")[2].split("=")[1]);
	var contentID = ((document.URL).split("&")[2].split("=")[1]);			
	//location.href = "zzcomment.html?phoneKey="+phoneKey+"&phoneNumber="+phoneNumber+"&cateID="+cateID+"&contentID="+contentID+"";
	
	if(isiOS) {
		
		window.webkit.messageHandlers.pushAllComments.postMessage([contentID,cateID,conNum])
	}else{
		window.AndroidWebView.openAllComments(contentID,cateID); //安卓查看全部评论
	}	
	
});


//点击回复评论
//$(".tuwen_pinglun_comment_center").on("singleTap",".tuwen_pinglun_parts_right_bottom1",function(){
//	var cateID = ((document.URL).split("&")[2].split("=")[1]);
//	var contentID = ((document.URL).split("&")[2].split("=")[1]);	
//	var commentid = $(this).attr("commentid");
//	var useN = $(this).attr("username");
	
//	if(isiOS){
//		window.webkit.messageHandlers.commentUserComments.postMessage([commentid,useN,contentID,cateID])
//	}else{
//		window.AndroidWebView.getUserComments(commentid,useN,contentID,cateID); //安卓回复个人
//	}
//});
//点击查看全部的回复评论
$(".tuwen_pinglun_comment_center").on("singleTap",".tuwen_pinglun_parts_right_bottom1_dsu",function(){
	var cateID = ((document.URL).split("&")[2].split("=")[1]);
	var contentID = ((document.URL).split("&")[2].split("=")[1]);	
	var commentid = $(this).parent().attr("commentid");
	var useN = $(this).parent().attr("username");	
	var num = parseInt($(this).html().replace(/[^0-9]/ig,""));
	
	
	if(isiOS){
		window.webkit.messageHandlers.lookUserComments.postMessage([commentid,contentID,useN,num,cateID])
	}else{
		window.AndroidWebView.seeUserAllComments(commentid,useN,contentID,cateID); //查看回复个人全部评论
	}
	
	
	return false;
	
});


//点击文章下头像
$('.tuwen_user_head').on("singleTap", function() {
	var phoneKey    = ((document.URL).split("&")[0].split("=")[1]);
	var phoneNumber = ((document.URL).split("&")[1].split("=")[1]);
	var userID = $(this).attr("user");			
	
	//location.href = "homepage.html?phoneKey="+phoneKey+"&phoneNumber="+phoneNumber+"&userID="+userID+""
	if(isiOS) {
		window.webkit.messageHandlers.clickHead.postMessage(userID);//给ios传递userID,ios
	}else{
		window.AndroidWebView.toAvatar(userID); //安卓隐藏	
	}
});
//评论区头像
$('.tuwen_pinglun_comment_center').on("singleTap", ".tuwen_pinglun_parts_left",function() {
	var phoneKey    = ((document.URL).split("&")[0].split("=")[1]);
	var phoneNumber = ((document.URL).split("&")[1].split("=")[1]);
	var userID = $(this).attr("user");			

	//location.href = "homepage.html?phoneKey="+phoneKey+"&phoneNumber="+phoneNumber+"&userID="+userID+""
	if(isiOS) {
		window.webkit.messageHandlers.clickHead.postMessage(userID);//给ios传递userID
	}else{
		window.AndroidWebView.toAvatar(userID); //安卓隐藏	
	}
});



//点击打赏
$(".tuwen_body_dashangbutton img").on('singleTap', function() {
	$(".pinglun_send").html("该功能暂未开放")
		$(".pinglun_send").css("display", "block");
		$(".pinglun_send").stop(true, true).animate({
			opacity: 0
		}, 2000, function() {
			$(this).css({
				opacity: ".8",
				display: "none"
			})
	})
	
//	var phoneKey    = ((document.URL).split("&")[0].split("=")[1]);
//	var phoneNumber = ((document.URL).split("&")[1].split("=")[1]);
//	var cateID = ((document.URL).split("&")[2].split("=")[1]);
//	var contentID = ((document.URL).split("&")[2].split("=")[1]);	
//	
//	if(phoneNumber==15234368069){
//		location.href = "22.html"
//	}else{
//		location.href = "xreward.html?phoneKey="+phoneKey+"&phoneNumber="+phoneNumber+"&cateID="+cateID+"&contentID="+contentID
//	
//	}
//	
//	
//	if(isiOS) {
//		window.webkit.messageHandlers.closeNav.postMessage("123")
//	}else{
//		window.AndroidWebView.showOrHideToolbar(0); //安卓隐藏	
//	}
});


function back() {
	//返回上一级
	if(isiOS) {
		
		window.webkit.messageHandlers.closeValue.postMessage('123');//ios退出返回	
	} else {
		window.AndroidWebView.closeValue("fengfeng"); //安卓		
	}
}

//搜索界面取消
$(".inhomesearch_quxiao_two").on("singleTap",function(){
	back();
});
//退出到ios
$(".footer_quit").on('singleTap', function() {
	back();
});

//问答返回上一级
$(".footer_quit1").on('singleTap', function() {
	history.go(-1);	
});

$(".history_foot_tuichu").on('singleTap', function() {
	history.go(-1);
});


// 测试本地解析
function out() {	
	var inputText = $('.text').val();
}

var html;

function reply(content) {
	html = `<li class="tuwen_pinglun_parts">
								<div class="tuwen_pinglun_parts_left">
									<img src="img/touxiang4.png"/>
								</div>
								<div class="tuwen_pinglun_parts_right">
									<div class="tuwen_pinglun_parts_right_top">
										<div class="tuwen_pinglun_names">
											乐观的无可儿
										</div>
										<a href="javascript:;">
											<span class="tuwen_pinglun_praiseImg"></span>
											<div class="tuwen_pinglun_pariseVal">134</div>																				
										</a>										
									</div>
									<div class="tuwen_pinglun_parts_right_mid">
										<div class="tuwen_pinglun_clockImg"></div>
										<div class="tuwen_pinglun_TimeVal">刚刚</div>
									</div>
								</div>
								<div class="tuwen_pinglun_parts_right_ti"></div>
								<div class="tuwen_pinglun_parts_right_bottom1">
									` + content + `								
								</div>								
							</li>`
	return html;
}

//点击评论框
	
$(".pinglun_footerr_search").on("touchstart",function() {
		
	$(".fthch").css({display: "block"});
	$('.pinglun_footerr_text').focus();
	$(".pinglun_footerr_text").attr("placeholder","一段话一张照片，记录你的幸福时刻!");
//	if(isiOS) {
//		//window.webkit.messageHandlers.closeNav.postMessage("123")
//	}else{
//		window.AndroidWebView.showOrHideToolbar(0); //安卓隐藏	
//	}		
});




//点击退出评论
$(".titt_tui").bind("touchend", function() {
	
	$(".fthch").css({display: "none"});
	
//	if(isiOS){
//			window.webkit.messageHandlers.openNav.postMessage("123")
//		}else{
//		window.AndroidWebView.showOrHideToolbar(1); //安卓隐藏	
//	}
	
});

//监控字数
$('.pinglun_footerr_text').on("input propertychange", function() {
	var $this = $(this),
		_val = $this.val(),
		count = "";
	if(_val.length > 0) {
		$(".surin").css({
			backgroundColor: "#6db0ff",
			color: "white"
		})
	} else {
		$(".surin").css({
			backgroundColor: "grey",
			color: "black"
		})
		
	}
	if(_val.length > 500) {
		$this.val(_val.substring(0, 500));
	}
	count = 500 - $this.val().length;
	$(".pinglun_footerr_tex_num em").text(count);
});

//评论 

$(".send_agrin").bind("touchstart", function() {
	$(this).css({"backgroundColor": "grey"});
	$(".pinglun_send").stop(true, true)
});
$(".send_agrin").bind("touchend", function() {
	
	if(isiOS){
		//window.webkit.messageHandlers.openNav.postMessage("123")
	}else{
		window.AndroidWebView.showOrHideToolbar(1); //安卓
	}
	
	setTimeout('$(".fthch").css({display:"none"});', 100);
	
	$(this).css({"backgroundColor": "#6db0ff"});

	if($('.pinglun_footerr_text').val() == "") {
		$(".pinglun_send").html("请输入内容")
		$(".pinglun_send").css("display", "block");
		$(".pinglun_send").stop(true, true).animate({
			opacity: 0
		}, 2000, function() {
			$(this).css({
				lineHeight:".5rem",
				opacity: ".8",
				display: "none"
			})
		})
	};
	if($('.pinglun_footerr_text').val() !== "") {
		out();
		//$('.tuwen_pinglun_comment_center').append(spin);

		var phoneKey    = ((document.URL).split("&")[0].split("=")[1]);
		var phoneNumber = ((document.URL).split("&")[1].split("=")[1]);
		var cateID = ((document.URL).split("&")[2].split("=")[1]);
		var contentID = ((document.URL).split("&")[2].split("=")[1]);				
		
	
		//转码解析输入法自带表情
		function utf16toEntities(str) {
            var patt=/[\ud800-\udbff][\udc00-\udfff]/g;
            // 检测utf16字符正则
            str = str.replace(patt, function(char){
                var H, L, code;
                if (char.length===2) {
                    H = char.charCodeAt(0);
                    // 取出高位
                    L = char.charCodeAt(1);
                    // 取出低位
                    code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00;
                    // 转换算法
                    return "&#" + code + ";";
                } else {
                    return char;
                }
            });
            return str;
        }
		
		var txtxt = utf16toEntities($('.text').val());
		
		
		
		$.ajax({
				url:"http://dev.onetuu.com:8080/python/api/comment",
				type: 'GET',
				dataType: 'jsonp', //指定为jsonp类型       		
				async:false,
				data: { //数据参数
					"phoneKey": phoneKey,
					"phoneNumber": phoneNumber,
					"cateID":cateID,
					"contentID":contentID,
					"comment":txtxt					
				},
				success: function(data) {
					sing();					
				},
				error:function(xx){
					console.log(123)
				}
			});
		
		
		
		
		$(".pinglun_footerr_text").val("");
		$(".pinglun_send").html("发送成功");		
		$(".pinglun_send").css("display", "block");
	
		$(".pinglun_send").animate({
			opacity: 0
		}, 1500, function() {
			$(this).css({
				opacity: ".8",
				display: "none"
			})
		})
	};

});
//点击关注	
var thaa;
$(".tuwen_user_follow").on('singleTap', function() {
	thaa = $(this);	
	var floId = thaa.parent().children().next().attr("user");
	if($(this).html()=="+关注") {	
		
		if(isiOS){
			window.webkit.messageHandlers.iosAddfriend.postMessage(floId);//ios加关注		
		}else{			
			window.AndroidWebView.focus(floId); //安卓						
		}
	}	
});

//此函数,再点击发送评论时,实现局部刷新
function sing(){
	
	$(".tuwen_pinglun_allTxt").css({"display":"block"});
	var phoneKey    = ((document.URL).split("&")[0].split("=")[1]);
	var phoneNumber = ((document.URL).split("&")[1].split("=")[1]);
	var cateID = ((document.URL).split("&")[2].split("=")[1]);
	var contentID = ((document.URL).split("&")[2].split("=")[1]);				
	$.ajax({
			type:'GET', 
	        url:'http://dev.onetuu.com:8080/python/api/recommend',       //跨域到https://www.wp.com，另，https://test.com也算跨域	                                       //jsonp 类型下只能使用GET,不能用POST,这里不写默认为GET
	        async:false,
	        dataType:'jsonp',			                //指定为jsonp类型
	        data:{									   //数据参数						
				"phoneKey": phoneKey,
				"phoneNumber": phoneNumber,
				"cateID":cateID,
				"contentID":contentID
	        },	       
	        success:function fcv(data){	        	
	        	var commentHt=""; 
				conNum = data.data.commentInfo.length;
	        	data.data.recommentInfo.forEach(function(v, k) {
	        		commentHt += `<div class="tuwen_dajiazaikan_else_center" wenFlag="`+v.flag+`" contentid="`+v.contentID+`">
						<div class="tuwen_dajiazaikan_imgWrap"><img src="http://pic.visionbang.com/?url=`+v.faceImg+`"></div>
						<p class="tuwen_dajiazaikan_til">`+v.title+`</p>
						<span class="tuwen_dajiazaikan_mes">`+v.abstract+`</span></div>`
	        	})
	        	//评论
				var pinHt="";
	        	var tt;
	        	data.data.commentInfo.length>=5?tt=5:tt=data.data.commentInfo.length;	
	        	for(var ta=0;ta<tt;ta++){
	        		
	        		pinHt += `<li class="tuwen_pinglun_parts">
						<div class="tuwen_pinglun_parts_left" user="`+data.data.commentInfo[ta].userid+`">
							<img src="`+data.data.commentInfo[ta].userImg+`"/>
						</div>
						<div class="tuwen_pinglun_parts_right">
							<div class="tuwen_pinglun_parts_right_top">
								<div class="tuwen_pinglun_names">
									`+data.data.commentInfo[ta].username+`
								</div>
								<a href="javascript:;">
									<span class="tuwen_pinglun_praiseImg"></span>
									<div class="tuwen_pinglun_pariseVal">`+data.data.commentInfo[ta].click+`</div>
								</a>
							</div>
							<div class="tuwen_pinglun_parts_right_mid">
								<div class="tuwen_pinglun_clockImg"></div>
								<div class="tuwen_pinglun_TimeVal">`+time(data.data.commentInfo[ta].date)+`</div>
							</div>
						</div>
						<div class="tuwen_pinglun_parts_right_ti"></div>
						<div class="tuwen_pinglun_parts_right_bottom1">
							`+AnalyticEmotion(data.data.commentInfo[ta].comment)+`
						</div>
					</li>`
	        	};	
	        	//2
				$(".tuwen_us").html('<div class="tuwen_dajiazaikan_else"><div class="tuwen_dajiazaikan_topText">大家还在看</div></div>'+commentHt)
				$(".tuwen_dajiazaikan_topText em").html("("+conNum+")");
				$(".tuwen_pinglun_comment_center").html(pinHt);						
				
				
				//回到底部
	        	$(document).scrollTop($(document).height()-$(window).height());
	        	
	        },			        			        
	})	
};

})